package cn.gemiman.daoyi.module.cms.dal.mysql.tagdictionary;

import java.util.*;

import cn.gemiman.daoyi.framework.common.pojo.PageResult;
import cn.gemiman.daoyi.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.gemiman.daoyi.framework.mybatis.core.mapper.BaseMapperX;
import cn.gemiman.daoyi.module.cms.dal.dataobject.blogmessage.BlogMessageDO;
import cn.gemiman.daoyi.module.cms.dal.dataobject.tagdictionary.TagDictionaryDO;
import org.apache.ibatis.annotations.Mapper;
import cn.gemiman.daoyi.module.cms.controller.admin.tagdictionary.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 文章标签 Mapper
 *
 * @author 道一源码
 */
@Mapper
public interface TagDictionaryMapper extends BaseMapperX<TagDictionaryDO> {

    default PageResult<TagDictionaryDO> selectPage(TagDictionaryPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TagDictionaryDO>()
                .likeIfPresent(TagDictionaryDO::getName, reqVO.getName())
                .eqIfPresent(TagDictionaryDO::getArticleCount, reqVO.getArticleCount())
                .betweenIfPresent(TagDictionaryDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TagDictionaryDO::getId));
    }

    @Update("update cms_tag_dictionary set article_count = article_count + 1 where name = #{name}")
    void updateArticleCount(@Param("name") String name);

    @Select("select ctd.id, ctd.name, (select count(1) from cms_blog_message cbm where cbm.tag = ctd.name) as article_count " +
            "from cms_tag_dictionary ctd " +
            "order by article_count desc")
    List<TagDictionaryDO> selectHotMessageTagList(@Param("appId") String appId);
}
